package com.loongtech.bi.action.pay;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.primefaces.event.TabChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.entity.pay.EntityNewUserLTV;
import com.loongtech.bi.manager.pay.UserPayManager;
import com.loongtech.bi.manager.pay.UserPayManager.UserPayCompareData;
import com.loongtech.bi.manager.pay.UserPayManager.UserPayData;
import com.loongtech.bi.manager.pay.UserPayManager.UserPayValueData;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;

/**
 * 付费分析 —新玩家价值Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class UserPayAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	public final static String platformCompareTab = "platform";  // 新玩家价值对比标签页-平台
	public final static String channelCompareTab = "channel";  // 新玩家价值对比标签页-渠道
	public final static String serverCompareTab = "server";  // 新玩家价值对比标签页-警服
	public String compareTab = platformCompareTab;  // 当前选中的新玩家价值对比标签页，默认是平台
	
	private List<EntityNewUserLTV> allUserPayValueData;  // 所有数据，用于组织下面的数据
	private List<UserPayData> userPayData;  // 前端玩家价值总体数据 
	private ChartUIData userPayChart;  // 前端新玩家价值总体数据图表
	private List<UserPayValueData> userPayValueData;  // 前端新玩家价值数据
	private ChartUIData userPayValueChart;  // 前端新玩家价值图表
	private List<UserPayCompareData> userPayCompareData;  // 前端新玩家价值对比数据
	private ChartUIData userPayCompareChart;  // 前端新玩家价值对比图表
	
	@Autowired
	private UserPayManager userPayManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 14 * Time.DAY));
		search();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "付费分析 —新玩家价值");
		
		userPayData = new ArrayList<>();
		userPayChart = new ChartUIData();
		userPayValueData = new ArrayList<>();
		userPayValueChart = new ChartUIData();
		userPayCompareData = new ArrayList<>();
		userPayCompareChart = new ChartUIData();
		
		allUserPayValueData = userPayManager.getAllUserPayValueData(dateStart, dateEnd);  // 查出所有结果
		if (allUserPayValueData != null && allUserPayValueData.size() > 0) {
			// 更新汇总和详细数据
			userPayManager.updateData(allUserPayValueData, dateStart, dateEnd, queryPlatform, queryChannel, queryServer, userPayData, userPayChart, userPayValueData, userPayValueChart);
			if (userPayChart.isNull()) {
				userPayChart = null;
			}
			if (userPayValueChart.isNull()) {
				userPayValueChart = null;
			}
			if (userPayCompareChart.isNull()) {
				userPayCompareChart = null;
			}
			
			// 更新对比数据
			userPayManager.updateData2(compareTab, allUserPayValueData, dateStart, dateEnd, queryPlatform, queryChannel, queryServer, userPayCompareData, userPayCompareChart);
			if (userPayCompareChart.isNull()) {
				userPayCompareChart = null;
			}
		} else {
			userPayChart = null;
			userPayValueChart = null;
			userPayCompareChart = null;
		}
	}
	
	// 切换新玩家价值对比标签页
	public void onCompareTabChange(TabChangeEvent event) {
		String eventTagId = event.getTab().getId();
		
		if (eventTagId != null && eventTagId.startsWith(channelCompareTab)) {
			compareTab = channelCompareTab;
		} else if (eventTagId != null && eventTagId.startsWith(serverCompareTab)) {
			compareTab = serverCompareTab;
		} else {
			compareTab = platformCompareTab;
		}
		
		userPayCompareData = new ArrayList<>();
		userPayCompareChart = new ChartUIData();
		userPayManager.updateData2(compareTab, allUserPayValueData, dateStart, dateEnd, queryPlatform, queryChannel, queryServer, userPayCompareData, userPayCompareChart);
		if (userPayCompareChart.isNull()) {
			userPayCompareChart = null;
		}
    }
         
	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public List<EntityNewUserLTV> getAllUserPayValueData() {
		return allUserPayValueData;
	}

	public void setAllUserPayValueData(List<EntityNewUserLTV> allUserPayValueData) {
		this.allUserPayValueData = allUserPayValueData;
	}

	public List<UserPayData> getUserPayData() {
		return userPayData;
	}

	public void setUserPayData(List<UserPayData> userPayData) {
		this.userPayData = userPayData;
	}

	public List<UserPayValueData> getUserPayValueData() {
		return userPayValueData;
	}

	public void setUserPayValueData(List<UserPayValueData> userPayValueData) {
		this.userPayValueData = userPayValueData;
	}

	public ChartUIData getUserPayValueChart() {
		return userPayValueChart;
	}

	public void setUserPayValueChart(ChartUIData userPayValueChart) {
		this.userPayValueChart = userPayValueChart;
	}

	public List<UserPayCompareData> getUserPayCompareData() {
		return userPayCompareData;
	}

	public void setUserPayCompareData(List<UserPayCompareData> userPayCompareData) {
		this.userPayCompareData = userPayCompareData;
	}

	public ChartUIData getUserPayCompareChart() {
		return userPayCompareChart;
	}

	public void setUserPayCompareChart(ChartUIData userPayCompareChart) {
		this.userPayCompareChart = userPayCompareChart;
	}

	public ChartUIData getUserPayChart() {
		return userPayChart;
	}

	public void setUserPayChart(ChartUIData userPayChart) {
		this.userPayChart = userPayChart;
	}
}
